package com.right.health.app.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.common.SendAuthCodeCommon;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.UserApi;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class ForgetPwdStep1Activity extends BaseActivity implements View.OnClickListener, OnHttpListener {
    private static final int HTTP_CODE_GET_CODE = 1;
    private static final int HTTP_CODE_VERIFY = 2;

    private SendAuthCodeCommon mSendAuthCodeCommon;

    private TextView mVerificationTv;

    private EditText mPhoneEt, mCodeEt;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.forget_pwd_step1);
    }

    @Override
    public void findView() {
        mPhoneEt = (EditText) findViewById(R.id.et_phone);
        mCodeEt = (EditText) findViewById(R.id.et_code);

        findViewById(R.id.btn_next).setOnClickListener(this);

        mVerificationTv = (TextView) findViewById(R.id.btn_verification);
        mVerificationTv.setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        mSendAuthCodeCommon = new SendAuthCodeCommon(milliseconds -> {
            mVerificationTv.setText(milliseconds == 0 ? getString(R.string.again_send_code) :
                    String.format(getString(R.string.format_seconds),
                            String.valueOf(milliseconds / 1000)));

            if (milliseconds == 0) {
                mVerificationTv.setClickable(true);
            }
        });
    }

    private boolean verifyEnter() {
        String phone = mPhoneEt.getText().toString().trim();
        if (!VerifyUtil.isMobile(phone)) {
            ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
            return false;
        }
        String code = mCodeEt.getText().toString().trim();
        if(TextUtils.isEmpty(code)) {
            ToastUtil.show(App.getInstance(),R.string.please_enter_code);
            return false;
        }

        return true;
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.btn_next) {
            if(verifyEnter()) {
                String phone = mPhoneEt.getText().toString().trim();
                String code = mCodeEt.getText().toString().trim();
                mUserApi.addRequestCode(HTTP_CODE_VERIFY);
                mUserApi.verifyFindPwdCode(phone, code);
            }
//            startIntent(ForgetPwdStep2Activity.class);
        } else if (v.getId() == R.id.btn_verification) {
            mVerificationTv.setClickable(false);
            String phone = mPhoneEt.getText().toString().trim();
            if (!VerifyUtil.isMobile(phone)) {
                mVerificationTv.setClickable(true);
                ToastUtil.show(App.getInstance(), R.string.please_enter_phone);
                return;
            }
            mUserApi.addRequestCode(HTTP_CODE_GET_CODE);
            mUserApi.getFindPwdCode(phone);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseOK()) {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                ToastUtil.show(App.getInstance(), "验证码发送成功");
                mSendAuthCodeCommon.startCountdown();
            } else if(request.requestCode == HTTP_CODE_VERIFY) {
                ToastUtil.show(App.getInstance(), "验证成功");

                String phone = mPhoneEt.getText().toString().trim();

                Bundle bundle = new Bundle();
                bundle.putString(BroadCastKey.DATA, phone);
                startIntent(ForgetPwdStep2Activity.class,bundle);
                finish();
            }

        } else {
            if(request.requestCode == HTTP_CODE_GET_CODE) {
                mVerificationTv.setClickable(true);
            }
            ToastUtil.show(App.getInstance(), response.error);
        }
    }
}
